<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 用户管理模块
 */
class Members extends Admin_Controller {
	
	function __construct()
	{
	    parent::__construct();
            $this->load->model('users_model');
	}
        //用户列表
	public function index()
	{	
            $where = array();
            if($_GET['ok']){
                $where['is_new'] = $_GET['ok'] == 2 ? 0 : $_GET['ok'];
            }
            
            
            if($_GET['st'] && $_GET['q']){
                $keyword = trim($_GET['q']);
                $type = trim($_GET['st']);
                if($type == 1){
                    if(preg_match('/^\d+$/i', $keyword)){
                        $user = $this->users_model->get_user_by_uid($keyword);
                    }else{
                        $user = $this->users_model->get_user_by_username($keyword);
                    }
                    $where['user_id'] = $user['user_id'];
                    $icon = '用户名/ID';
                }elseif($type == 2){//手机号
                    $icon = '手机号';
                    $like = $keyword;
                }elseif($type == 3){//邮箱
                    $icon = '邮箱';
                    $where['email'] = $keyword;
                }else{//推荐人
                    $icon = '推荐人';
                    $spread = $this->users_model->get_user_by_username($keyword);
                    $spread ? $where['parent_id'] = $spread['user_id'] : '';
                }
            }
            
            if($_GET['time-start']){
                $where['reg_time >='] = strtotime($_GET['time-start']);
            }
            if($_GET['time-end']){
                $where['reg_time <='] = strtotime($_GET['time-end']) + 3600*24;
            }
            
            //总数
            $total = $this->users_model->get_count($where,$like);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
                array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            //记录
            $list = $this->users_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
                $records_per_page,$like);
            foreach ($list as $key=>$val){
                $where = array();
                $where['user_id'] = $val['parent_id'];
                $spreader = $val['parent_id'] ? $this->users_model->get_spreader($where) : '';
                $spreader_name = $val['parent_id'] ? $spreader['username'] : '';
                $list[$key]['spreader_name'] = $spreader_name;
                $list[$key]['status'] = $val['status'] == 1  ? '正常' : '锁定';
                $list[$key]['is_new'] = $val['is_new'] == 1  ? '已完成' : '未完成';
                $list[$key]['vip_degree'] = $val['vip_degree']."级";
                $list[$key]['reg_time'] = date('Y-m-d H:i:s',$val['reg_time']);
            }
            $this->template->assign('keyword', $_GET['q']);
            $this->template->assign('timestart', $_GET['time-start']);
            $this->template->assign('timeend', $_GET['time-end']);
            $this->template->assign('is_new', $_GET['ok']);
            $this->template->assign('icon', $icon);
            $this->template->assign('icon_type', $type);
            $this->template->assign('Total', $total);
            $this->template->assign('showpage', $this->xpage->render(true));
            $this->template->assign('list', $list);
            $this->template->load('members/index');
	}
        
        //用户管理
        function manager(){
            $where = array();
            if($_GET['ok']){
                $where['is_new'] = $_GET['ok'] == 2 ? 0 : $_GET['ok'];
            }
            
            
            if($_GET['st'] && $_GET['q']){
                $keyword = trim($_GET['q']);
                $type = trim($_GET['st']);
                if($type == 1){
                    if(preg_match('/^\d+$/i', $keyword)){
                        $user = $this->users_model->get_user_by_uid($keyword);
                    }else{
                        $user = $this->users_model->get_user_by_username($keyword);
                    }
                    $where['user_id'] = $user['user_id'];
                    $icon = '用户名/ID';
                }elseif($type == 2){//手机号
                    $icon = '手机号';
                    $like = $keyword;
                }elseif($type == 3){//邮箱
                    $icon = '邮箱';
                    $where['email'] = $keyword;
                }else{//推荐人
                    $icon = '推荐人';
                    $spread = $this->users_model->get_user_by_username($keyword);
                    $spread ? $where['parent_id'] = $spread['user_id'] : '';
                }
            }
            
            if($_GET['time-start']){
                $where['reg_time >='] = strtotime($_GET['time-start']);
            }
            if($_GET['time-end']){
                $where['reg_time <='] = strtotime($_GET['time-end']);
            }
            
            //总数
            $total = $this->users_model->get_count($where,$like);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
                array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            //记录
            $list = $this->users_model->get_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
                $records_per_page,$like);
            foreach ($list as $key=>$val){
                $where = array();
                $where['user_id'] = $val['parent_id'];
                $spreader = $val['parent_id'] ? $this->users_model->get_spreader($where) : '';
                $spreader_name = $val['parent_id'] ? $spreader['username'] : '';
                $list[$key]['spreader_name'] = $spreader_name;
                //$list[$key]['status'] = $val['status'] == 1  ? '正常' : '锁定';
                $list[$key]['is_new'] = $val['is_new'] == 1  ? '已完成' : '未完成';
                $list[$key]['vip_degree'] = $val['vip_degree']."级";
                $list[$key]['reg_time'] = date('Y-m-d H:i:s',$val['reg_time']);
            }
            $this->template->assign('keyword', $_GET['q']);
            $this->template->assign('timestart', $_GET['time-start']);
            $this->template->assign('timeend', $_GET['time-end']);
            $this->template->assign('is_new', $_GET['ok']);
            $this->template->assign('icon', $icon);
            $this->template->assign('icon_type', $type);
            $this->template->assign('Total', $total);
            $this->template->assign('showpage', $this->xpage->render(true));
            $this->template->assign('list', $list);
            $this->template->load('members/manager');
        }
        
        //设置用户锁定状态
        function setstatus(){
            if($_GET){
                $mssage = array();
                $user_id	   = trim($_GET['id']);
                $is_status = trim($_GET['is_status']);
                $data = array();
                $data['status'] = $is_status;
                $result = $this->users_model->user_update($data,$user_id);
                if($result){
                    if($is_status==1){
                        $mssage['info']	  = '解锁成功';
                    }else{
                        $mssage['info']	  = '锁定成功';
                    }
                    $mssage['status'] = 1;
                    $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                    echo json_encode($mssage);exit;
                }else{
                    $mssage['info']	  = '操作失败';
                    $mssage['status'] = 0;
                    $mssage['url']	  = $_SERVER['HTTP_REFERER'];
                    echo json_encode($mssage);exit;
                }
            }	
        }
        
        //用户反馈
        function feedback(){
            $where = array();
            if($_GET['st'] ){
                $type = trim($_GET['st']);
                if($type == 1){//手机号
                    $icon = '体验反馈';
                    $where['feed_type'] = 1;
                }elseif($type == 2){//邮箱
                    $icon = '功能反馈';
                    $where['feed_type'] = 2;
                }else{//推荐人
                    $icon = 'BUG反馈';
                    $where['feed_type'] = 3;
                }
            }
            
            if($_GET['time-start']){
                $where['create_time >='] = strtotime($_GET['time-start']);
            }
            if($_GET['time-end']){
                $where['create_time <='] = strtotime($_GET['time-end']);
            }
            //总数
            $total = $this->users_model->get_feedback_count($where);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
                array('left', 'right')) ? $navigation_position : 'outside');
            //$this->xpage->reverse(true);
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            //记录
            $list = $this->users_model->get_feedback_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
                $records_per_page);
            foreach ($list as $key=>$val){
                $list[$key]['create_time'] = date('Y-m-d H:i:s',$val['create_time']);
                $list[$key]['feed_type'] = $val['feed_type'] == 1 ? "体验反馈" : ( $val['feed_type'] == 2 ? "功能反馈" : "BUG反馈");
            }
            $this->template->assign('keyword', $_GET['q']);
            $this->template->assign('timestart', $_GET['time-start']);
            $this->template->assign('timeend', $_GET['time-end']);
            $this->template->assign('icon', $icon);
            $this->template->assign('icon_type', $type);
            $this->template->assign('Total', $total);
            $this->template->assign('showpage', $this->xpage->render(true));
            $this->template->assign('list', $list);
            $this->template->load('members/feedback');
        }
        
        //线下用户列表
        function lineuser(){
            $where = array();
            if($_GET['st'] && $_GET['q']){
                $keyword = trim($_GET['q']);
                $type = trim($_GET['st']);
                if($type == 1){//手机号
                    $icon = '手机号';
                    $$where['mobile'] = $keyword;
                }elseif($type == 2){//邮箱
                    $icon = '邮箱';
                    $where['email'] = $keyword;
                }
            }
            //总数
            $total = $this->users_model->get_users_line_count($where,$like);
            $navigation_position = 'outside';
            $records_per_page = 10;
            $this->xpage->navigation_position(isset($navigation_position) && in_array($navigation_position,
                array('left', 'right')) ? $navigation_position : 'outside');
            $this->xpage->records($total);
            $this->xpage->records_per_page($records_per_page);
            $this->xpage->labels("<<", ">>");
            //记录
            $list = $this->users_model->get_users_line_list($where, (($this->xpage->get_page() - 1) * $records_per_page),
                $records_per_page,$like);
            $this->template->assign('keyword', $_GET['q']);
            $this->template->assign('icon', $icon);
            $this->template->assign('icon_type', $type);
            $this->template->assign('Total', $total);
            $this->template->assign('showpage', $this->xpage->render(true));
            $this->template->assign('list', $list);
            $this->template->load('members/lineuser');
    }
    
    //导入线下数据表
    function import_users_line(){
        if($_POST){
            $my_excel = new Mexcel();
            $file_name = trim($_POST['file_url']);
            $result = $my_excel->excel_to_array($file_name);
            if($result['status'] == 1 ){
                $list_ = $result['data'];
                if(!$list_){
                    $mess['status'] = 1;
                    $mess['error'] = '没有发现任何数据';
                    echo json_encode($mess);die;
                }
                $list = array();
                $flag_name = 0;//判断平台名称是否正确
                $flag_phone = 0;//判断手机号格式是否正确
                $flag_str = "";
                foreach ($list_ as $key=>$val){
                    if(!preg_match("/^1[3|4|5|7|8]\d{9}$/", $val[0])){
                        $flag_str .= $val[0];
                        $flag_phone = 1;
                        break;
                    }

                    $temp = array();
                    $temp['mobile'] = $val[0];
                    $temp['email'] = $val[1];
                    $temp['true_name'] = $val[2];
                    $temp['ID_card'] = $val[3];
                    $list[] = $temp;
                }
                if($flag_phone){
                    $mess['status'] = 1;
                    $mess['error'] = '手机号码格式解析错误,第'.($key+1)."条：".$flag_str;
                    echo json_encode($mess);die;
                }
                
                $import_success = 0;//成功导入
                $import_fail = 0;//失败导入
                $import_success_str = '';//成功导入
                $import_fail_str = '';//失败导入
                $datas = array();
                foreach ($list as $key=>$val){
                    //检测是否存在有线上用户
                    $user = $this->users_model->get_user_by_mobile($val['mobile']);
                    //检测是否存在下线用户
                    $user_ = $this->users_model->get_user_line_by_mobile($val['mobile']);
                    if($user){
                        $import_fail_str.$val['mobile'].",";
                        $import_fail ++;
                    }elseif($user_){
                        $import_fail_str.$val['mobile'].",";
                        $import_fail ++;
                    }else{
                        $temp = array();
                        $temp['mobile'] = $val["mobile"];
                        $temp['email'] = $val["email"];
                        $temp['true_name'] = $val["true_name"];
                        $temp['ID_card'] = $val["ID_card"];
                        $datas[] = $temp;
                        $import_success_str.$val['mobile'].",";
                        $import_success ++;
                        
                    }
                }
                if($datas){
                    $this->users_model->insert_line_users($datas);
                }
                $mess['status'] = 0;
                $mess['info'] = '成功导入'.$import_success.'条数据['.$import_success_str.'],失败导入'.$import_fail.'条数据['.$import_fail_str.']';
            }else{
                $mess['status'] = 1;
                $mess['error'] = $result['error'];
            }
            
            echo json_encode($mess);die;
        }
    }
    
    //网站统计
    function tongji(){
        $today_time = strtotime(date("Y-m-d")." 00:00:00");//今日时间
        $yestoday_time = $today_time - 86400;//昨日时间
        $week_time = $this->get_self_week_time();//本周时间
        $month_time = strtotime(date("Y-m-00"));//本月时间
        
        
        $where = "reg_time > $today_time";
        $today_reg = $this->db->select("*")->where($where)->from("tan_users")->order_by("user_id","desc")->get()->num_rows();//今日注册用户
        $where = "reg_time > $yestoday_time and reg_time <= ".($yestoday_time + 86400);
        $yestoday_reg = $this->db->select("*")->where($where)->from("tan_users")->order_by("user_id","desc")->get()->num_rows();//昨日注册用户
        $where = "reg_time > $week_time";
        $week_reg = $this->db->select("*")->where($where)->from("tan_users")->order_by("user_id","desc")->get()->num_rows();//本周注册用户
        $where = "reg_time > $month_time";
        $month_reg = $this->db->select("*")->where($where)->from("tan_users")->order_by("user_id","desc")->get()->num_rows();//本月注册用户
        
        $total_reg_num = $this->db->select('*')->from('users')->order_by('user_id','desc')->get()->num_rows();
        
        $total_reg_num = number_format($total_reg_num - 3000);
        
        //总投资金额
        $total_invest_amount = $this->db->select('SUM(amount) total_amount')->from('plat_invest_list')->get()->row_array();
        $total_invest_amount = number_format(intval($total_invest_amount['total_amount']+244828480));
        
        $this->template->assign('today_reg',$today_reg);
        $this->template->assign('yestoday_reg',$yestoday_reg);
        $this->template->assign('week_reg',$week_reg);
        $this->template->assign('month_reg',$month_reg);
        $this->template->assign('total_reg_num',$total_reg_num);
        $this->template->assign('total_invest_amount',$total_invest_amount);
        $this->template->assign('segment_p','tongji');
        $this->template->load('members/tongji');
    }
    
    
    private function get_self_week_time(){
        //当前日期 
        $sdefaultDate = date("Y-m-d"); 
        //$first =1 表示每周星期一为开始日期 0表示每周日为开始日期 
        $first=1; 
        //获取当前周的第几天 周日是 0 周一到周六是 1 - 6 
        $w=date('w',strtotime($sdefaultDate)); 
        //获取本周开始日期，如果$w是0，则表示周日，减去 6 天 
        $week_start=date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days')); 
        return strtotime($week_start);
    }
}
